﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using net.veekee.common;
using net.veekee.common.data.excel;
using System.Collections;
using net.veekee.logprocessor.plugin.output;

namespace net.veekee.logprocessor.plugin.output.pack
{
    class XLSWriter : AWriter
    {
        private ExcelFile excel = null;
        private ulong currentLine = 0;

        public override bool Load(String filename)
        {
            try
            {
                this.FileName = filename;
                this.excel = new ExcelFile(this.FileName, this.context.Append);

                this.excel.SelectSheet("interceptor");

                this.WriteHeader();
                return this.excel.IsValid;
            }
            catch (Exception e)
            {
                Log.GetInstance().Error(e.ToString());
                this.WriteFooter();
            }
            return false;
        }

        protected override void AddLine(IEnumerator items)
        {
            if (!this.excel.IsValid)
                return;
            this.excel.SetLine(this.currentLine, items);
            this.currentLine++;
            if (this.currentLine % 100 == 0)
                this.excel.Save();
        }

        public override void WriteFooter()
        {
            if (!this.excel.IsValid)
                return;
            this.excel.Save();
        }

        public override void Dispose()
        {
            if (this.excel != null)
            {
                this.WriteFooter();
                this.excel.Dispose();
            }
        }
    }
}
